################################################################################
# Copyright IBM Corp. and others 2017
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License 2.0 which accompanies this
# distribution and is available at https://www.eclipse.org/legal/epl-2.0/
# or the Apache License, Version 2.0 which accompanies this distribution and
# is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# This Source Code may also be made available under the following
# Secondary Licenses when the conditions for such availability set
# forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
# General Public License, version 2 with the GNU Classpath
# Exception [1] and GNU General Public License, version 2 with the
# OpenJDK Assembly Exception [2].
#
# [1] https://www.gnu.org/software/classpath/license.html
# [2] https://openjdk.org/legal/assembly-exception.html
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
################################################################################

set(J9VM_JCL_BIN_DIR "${CMAKE_CURRENT_BINARY_DIR}")

add_tracegen(j9jcl.tdf)
add_tracegen(jcl_java.tdf)
add_tracegen(jcl_net.tdf)
add_tracegen(jcl_nio.tdf)

j9vm_add_library(jclse SHARED
	OUTPUT_NAME jclse${J9VM_VERSION_SUFFIX}
	${CMAKE_CURRENT_BINARY_DIR}/ut_j9jcl.c
)

target_include_directories(jclse
	PRIVATE
		${CMAKE_CURRENT_SOURCE_DIR}
		${CMAKE_CURRENT_SOURCE_DIR}/common
		${j9vm_SOURCE_DIR}/shared_common/include
		${CMAKE_CURRENT_BINARY_DIR}
)

if(OMR_OS_WINDOWS)
	target_include_directories(jclse INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/win32)
else()
	target_include_directories(jclse INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/unix)
endif()

# Generate constant pool files
set(CONST_POOL_OUT_DIR "${j9vm_BINARY_DIR}")

# List of files generated by J9VMCPTool
set(gen_constpool_files "${CONST_POOL_OUT_DIR}/j9vmconstantpool.c")
if(OMR_OS_WINDOWS)
	set(JAVA_PATH_SEP ";")
else()
	set(JAVA_PATH_SEP ":")
endif()

# Dump the current cmake cache to text file
# We could just use CMakeCache.txt, but this way filters out advanced/internal variables and comments
add_custom_command(
	OUTPUT parsed_cache.txt
	COMMAND "${CMAKE_COMMAND}" -N -L > "${CMAKE_CURRENT_BINARY_DIR}/parsed_cache.txt"
	VERBATIM
	WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
)
add_custom_command(
	OUTPUT
		${gen_constpool_files}
	COMMAND "${Java_JAVA_EXECUTABLE}"
		-cp "$<TARGET_PROPERTY:j9vmcp,JAR_FILE>${JAVA_PATH_SEP}$<TARGET_PROPERTY:objectmodel,JAR_FILE>"
		com.ibm.oti.VMCPTool.Main
			-rootDir "${j9vm_SOURCE_DIR}"
			-outputDir "${CONST_POOL_OUT_DIR}"
			-cmakeCache "${CMAKE_CURRENT_BINARY_DIR}/parsed_cache.txt"
			-version ${JAVA_SPEC_VERSION}
	VERBATIM
	DEPENDS
		${j9vm_SOURCE_DIR}/oti/vmconstantpool.xml
		"${CMAKE_CURRENT_BINARY_DIR}/parsed_cache.txt"
		j9vmcp
		objectmodel
)
add_custom_target(run_cptool
	DEPENDS ${gen_constpool_files}
)
add_dependencies(j9vm_interface run_cptool)
set(cp_file_name ${CONST_POOL_OUT_DIR}/j9vmconstantpool.c)
set_source_files_properties(${cp_file_name} PROPERTIES GENERATED TRUE)
target_sources(jclse PRIVATE ${cp_file_name})

target_link_libraries(jclse
	PRIVATE
		j9vm_interface
		j9vm_gc_includes

		omrsig
		j9hookable
		j9zlib
		j9util
		j9utilcore
		j9avl
		j9hashtable
		j9pool
		j9thr
		sunvmi
)

target_sources(jclse
	PRIVATE
		${CMAKE_CURRENT_SOURCE_DIR}/common/annparser.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/attach.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/bootstrp.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/bpinit.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/clsldr.cpp
		${CMAKE_CURRENT_SOURCE_DIR}/common/com_ibm_jvm_Stats.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/com_ibm_oti_vm_VM.cpp
		${CMAKE_CURRENT_SOURCE_DIR}/common/compiler.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/dump.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/exhelp.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/extendedosmbean.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/getstacktrace.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/gpu.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/iohelp.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/java_lang_Access.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/java_lang_Class.cpp
		${CMAKE_CURRENT_SOURCE_DIR}/common/java_lang_J9VMInternals.cpp
		${CMAKE_CURRENT_SOURCE_DIR}/common/java_lang_ref_Finalizer.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/java_lang_ref_Reference.cpp
		${CMAKE_CURRENT_SOURCE_DIR}/common/jclcinit.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/jcldefine.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/jclexception.cpp
		${CMAKE_CURRENT_SOURCE_DIR}/common/jclglob.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/jclreflect.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/jcltrace.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/jclvm.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/jithelpers.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/jniidcacheinit.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/log.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/mgmtclassloading.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/mgmtcompilation.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/mgmtgc.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/mgmthypervisor.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/mgmtinit.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/mgmtmemmgr.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/mgmtmemory.cpp
		${CMAKE_CURRENT_SOURCE_DIR}/common/mgmtmempool.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/mgmtos.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/mgmtosext.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/mgmtruntime.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/mgmtthread.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/orbvmhelpers.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/proxy.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/reflecthelp.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/shared.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/sigquit.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/stdinit.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/sun_misc_Perf.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/sun_misc_Unsafe.cpp
		${CMAKE_CURRENT_SOURCE_DIR}/common/sun_reflect_ConstantPool.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/system.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/thread.cpp
		${CMAKE_CURRENT_SOURCE_DIR}/common/unsafe_mem.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/vminternals.c
		${CMAKE_CURRENT_SOURCE_DIR}/common/vm_scar.c
		${CMAKE_CURRENT_SOURCE_DIR}/filesys/vmfilesys.c
)

if(OMR_OS_WINDOWS)
	target_sources(jclse PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/win32/syshelp.c)
else()
	target_sources(jclse PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/unix/syshelp.c)
endif()

if(J9VM_INTERP_SIG_USR2)
	target_sources(jclse PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/common/sigusr2.c)
endif()

if(J9VM_OPT_METHOD_HANDLE)
	target_sources(jclse
		PRIVATE
			${CMAKE_CURRENT_SOURCE_DIR}/common/java_dyn_methodhandle.c
			${CMAKE_CURRENT_SOURCE_DIR}/common/java_dyn_methodtype.c
	)
endif()

if(J9VM_OPT_OPENJDK_METHODHANDLE OR J9VM_OPT_METHOD_HANDLE)
	target_sources(jclse
		PRIVATE
			${CMAKE_CURRENT_SOURCE_DIR}/common/java_lang_invoke_MethodHandleNatives.cpp
	)
endif()

if(J9VM_OPT_CRIU_SUPPORT)
	target_sources(jclse
		PRIVATE
			${CMAKE_CURRENT_SOURCE_DIR}/common/criu.cpp
	)
endif()

if(JAVA_SPEC_VERSION EQUAL 8)
	# sources for Java 8 only
	target_sources(jclse
		PRIVATE
			${CMAKE_CURRENT_SOURCE_DIR}/common/sun_misc_URLClassPath.c
	)
endif()

# We'd like to use GREATER_EQUAL here (and below), but that operator requires
# cmake version 3.7 or better; the minimum version we require is 3.4.
if(NOT JAVA_SPEC_VERSION LESS 9)
	# sources for Java 9+
	target_sources(jclse
		PRIVATE
			${CMAKE_CURRENT_SOURCE_DIR}/common/java_lang_invoke_VarHandle.c
			${CMAKE_CURRENT_SOURCE_DIR}/common/java_lang_StackWalker.cpp
	)
endif()

# We'd like to use GREATER_EQUAL here (and below), but that operator requires
# cmake version 3.7 or better; the minimum version we require is 3.4.
if(NOT JAVA_SPEC_VERSION LESS 16)
	# sources for Java 16+
	target_sources(jclse
		PRIVATE
			${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_internal_foreign_abi_UpcallStubs.cpp
			${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_internal_misc_ScopedMemoryAccess.cpp
			${CMAKE_CURRENT_SOURCE_DIR}/common/vectornatives.cpp
	)
endif()

# We'd like to use GREATER_EQUAL here (and below), but that operator requires
# cmake version 3.7 or better; the minimum version we require is 3.4.
if(NOT JAVA_SPEC_VERSION LESS 19)
	# sources for Java 19+
	target_sources(jclse
		PRIVATE
			${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_internal_vm_Continuation.cpp
	)
endif()

if(JAVA_SPEC_VERSION LESS 24)
	target_sources(jclse PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/common/acccont.c)
endif()

if(J9VM_OPT_JFR)
	# JFR sources
	if(JAVA_SPEC_VERSION EQUAL 11)
		target_sources(jclse
			PRIVATE
				${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_jfr_internal_JVM_common.cpp
				${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_jfr_internal_JVM_jdk11.cpp
		)
	elseif(JAVA_SPEC_VERSION EQUAL 17)
		target_sources(jclse
			PRIVATE
				${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_jfr_internal_JVM_common.cpp
				${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_jfr_internal_JVM_jdk17.cpp
				${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_jfr_internal_JVM_jdk17andUp.cpp
		)
	elseif(JAVA_SPEC_VERSION EQUAL 21)
		target_sources(jclse
			PRIVATE
				${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_jfr_internal_JVM_common.cpp
				${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_jfr_internal_JVM_jdk17andUp.cpp
				${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_jfr_internal_JVM_jdk21andUp.cpp
		)
	elseif(NOT JAVA_SPEC_VERSION LESS 23)
		target_sources(jclse
			PRIVATE
				${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_jfr_internal_JVM_common.cpp
				${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_jfr_internal_JVM_jdk17andUp.cpp
				${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_jfr_internal_JVM_jdk21andUp.cpp
				${CMAKE_CURRENT_SOURCE_DIR}/common/jdk_jfr_internal_JVM_jdk23.cpp
		)
	endif()
endif()

include(exports.cmake)

target_enable_ddr(jclse)
ddr_set_add_targets(j9ddr jclse)

install(
	TARGETS jclse
	LIBRARY DESTINATION ${j9vm_SOURCE_DIR}
	RUNTIME DESTINATION ${j9vm_SOURCE_DIR}
)
